param($BuildType)
"Starting Build: $BuildType"

## Get location of crrent process directory
$ScriptRoot = (Get-Location -PSProvider FileSystem).ProviderPath

function NeedsBuild([string]$SrcFile, [string]$DstFile)
{
    if(test-path $DstFile)
    {
        $SrcInfo = Get-Item $SrcFile
        $DstInfo = Get-Item $DstFile
        $SrcInfo.LastWriteTime -gt $DstInfo.LastWriteTime
    }
    else
    {
        "Destination not found: $DstFile" | Out-Host
        $true
    }
}

$RefHxs = "FusionWare_Ref\GeneratedHelp\FusionWare_Ref.HxS"
$DocsHxs = "FusionWare_Docs\Build\Default\FusionWare_Docs.HxS"

pushd $ScriptRoot

if( $BuildType -ieq "CLEAN" -or $BuildType -ieq "REBUILD" )
{
    if(test-path $RefHxs)
    {
        del $RefHxs
    }
    
    if(test-path $DocsHxs) 
    {
        del $DocsHxs
    }
}

if( $BuildType -ieq "BUILD" -or $BuildType -ieq "REBUILD" )
{
    ## SHFB copies files into the working folder and tries to delete it before
    ## starting a build and after completion but it will fail if the logo is
    ## Read only for some reason...
    attrib -r FusionWare_Ref\FusionWareLogo.png
    if(test-Path FusionWare_Ref\GeneratedHelp)
    {
        attrib /S /D -R FusionWare_Ref\GeneratedHelp\*.*
    }

    if(NeedsBuild "FusionWare_Ref\GeneratedHelp.shfb" $RefHxs) 
    {
        "Building Reference documentation..."
        & "$env:PROGRAMFILES\EWSoftware\Sandcastle Help File Builder\SandcastleBuilderConsole.exe" FusionWare_Ref\GeneratedHelp.shfb
    }
    else
    {
        "$RefHxs is up to date"
    }

    if(NeedsBuild "FusionWare_Docs\FusionWare_SPOT.hsp" $DocsHxs)
    {
        "Building general docs..."
        & "$env:PROGRAMFILES\Innovasys\HelpStudioLite\bin\HelpStudioLite2.exe" /B /s FusionWare_Docs\FusionWare_SPOT.hsp
    }
    else
    {
        "$DocsHxs is up to date"
    }
}
popd